package com.softserveinc.interactive_board.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.softserveinc.interactive_board.domain.User;
import com.softserveinc.interactive_board.domain.UserType;


@Repository
public class InteractiveBoardDAOImpl implements InteractiveBoardDAO {
	
	public List<UserType> getAllUserTypes() {
		return sessionFactory.getCurrentSession().createQuery("from UserType").list();
	}
	
	
	public List<UserType> getRegistratebleUserTypes() {
		return sessionFactory.getCurrentSession().createQuery("from UserType u where u.id < 3").list();
	}

	
	public List<User> getAllUsers() {
		return sessionFactory.getCurrentSession().createQuery("from User").list();
	}
	
	
	public List<User> getUser(Long id) {
		return this.sessionFactory.getCurrentSession().createQuery("from User u where u.id = :id")
														.setString("id", id.toString()).list();
	}
	
	
	public boolean addUser(User user) {
		if (!sessionFactory.getCurrentSession().createQuery("from User u where u.login = :l")
												.setString("l", user.getLogin()).list().isEmpty())
			return false;
		sessionFactory.getCurrentSession().save(user);
		return true;
	}
	
	
	public void updateUser(User user) {
		sessionFactory.getCurrentSession().update(user);
	}
	
	
	public void deleteUser(Long userId) {
		this.sessionFactory.getCurrentSession().delete(new User(userId));
	}
	

    private SessionFactory sessionFactory;

	@Autowired
	@Qualifier("sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
